package com.rhb.cg.create;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.rhb.cg.distribute.CopyFile;

public class Create {
	public void eclipse(String from_name, String to_name, String create_definition_name, String projectName) throws IOException{
		File from_dir = new File(from_name);
		File to_dir = new File(to_name + "\\" + projectName);
		File create_definition_file = new File(create_definition_name);
		
		if(!create_definition_file.isFile()){
			abort(create_definition_name + " is NOT a file.");
		}
		
		if(to_dir.isDirectory()){
			abort(to_name + " has already exist.");
		}
		
		Map<String,String> updateMap = CreateDefinition.prase(create_definition_file);

		List files = new ArrayList();
		getFiles(files,from_dir);
		
		int copyed = 0;  
		int copyFailed = 0;
		int updated = 0;
		int updateFailed = 0;
		int length = from_dir.getPath().length();		
		Iterator i = files.iterator();
		while(i.hasNext()){
			String from_file_name = (String)i.next();
			String to_file_name = to_dir.getPath() + from_file_name.substring(length);
			
			StringBuffer sb = new StringBuffer();
			File from_file = new File(from_file_name);
			File to_file = new File(to_file_name);
			try{
				copyed++;
				sb.append("copy "+ from_file.getAbsolutePath() + "\n");
				sb.append("  to "+ to_file.getAbsolutePath() + "\n");
				CopyFile.copy(from_file, to_file);
				sb.append("Succeed.\n");
				sb.append("\n");
			}catch(Exception e){
				copyFailed++;
				e.printStackTrace();
				sb.append("Fail,\n");
				sb.append("\n");
			}
			p(sb.toString());
		}

		Iterator j = files.iterator();
		while(j.hasNext()){
			String from_file_name = (String)j.next();
			String to_file_name = to_dir.getPath() + from_file_name.substring(length);
			
			StringBuffer sb = new StringBuffer();
			File to_file = new File(to_file_name);
			if(updateMap.containsKey(to_file.getName())){
				try{
					updated ++;
					sb.append("update " + to_file.getAbsolutePath() + "\n");			
					UpdateFile updateFile = (UpdateFile) UpdateContext.getInstance().getAppContext().getBean(updateMap.get(to_file.getName()));
					updateFile.update(to_file, projectName);
					sb.append("Succeed.\n");
					sb.append("\n");
				}catch(Exception e){
					updateFailed++;
					e.printStackTrace();
					sb.append("   Fail,\n");
					sb.append("\n");
				}
			}
			p(sb.toString());
		}

		
		p("**** copyed " + (copyed-copyFailed) + " files, "+ copyFailed+" failed.");
		p("**** updated " + (updated-updateFailed) + " files, "+ updateFailed+" failed.");
	}
	
	private void getFiles(List files,File dir) throws IOException{
		String[] strs = dir.list();
		for(int i=0; i<strs.length; i++){
			File f = new File(dir.getPath()+"\\"+strs[i]);
			if(f.isDirectory()){
				getFiles(files,f);
			}else if(f.isFile()){
				files.add(dir.getPath()+"\\"+strs[i]);
			}else{
				abort("Do not know what is it - " + dir.getPath()+"\\"+strs[i]);
			}
		}
	}

	private static void abort(String msg) throws IOException{
		throw new IOException("create eclipse project error: " + msg);
	}
	
	private static void p(String msg){
		System.out.println(msg);
	}

}
